﻿using NetDimension.NanUI;
using NetDimension.NanUI.Browser;
using NetDimension.NanUI.HostWindow;
using NetDimension.NanUI.JavaScript;
using PLM.Configuration.Center.NanUIApp.Formiums;
using PLM.Configuration.Center.NanUIApp.Windows;
using PLM.ConfigurationCenter.Model;
using PLM.ConfigurationCenter.SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PLM.Configuration.Center.NanUIApp {
    public class MainWindow : Formium {
        // 设置窗体样式类型
        public override HostWindowType WindowType => HostWindowType.System;
        // 指定启动 Url
        public override string StartUrl => "http://res.app.local/";

        public MainWindow() {
            // 在此处设置窗口样式
            Size = new System.Drawing.Size(1024, 768);
            this.StartPosition = FormStartPosition.CenterScreen;
            Init();
        }
        /// <summary>
        /// 
        /// </summary>
        private static async void Init() {
            DbManager.DbContext.CodeFirst.InitTables<ServerModel>();
            var list = await DbManager.DbContext.Queryable<ServerModel>().ToListAsync();
            if (!list.Any()) {
                var arr = new List<ServerModel> {
                    new ServerModel { Tag = "核心服务", Id = "API", SiteLabel = "后端服务" },
                    new ServerModel { Tag = "核心服务", Id = "WEB", SiteLabel = "前端服务" },
                    new ServerModel { Tag = "核心服务", Id = "COMMONITEM", SiteLabel = "缓存服务" },
                    new ServerModel { Tag = "基础服务", Id = "REDIS", SiteLabel = "Redis服务" },
                    new ServerModel { Tag = "基础服务", Id = "FILESTORE", SiteLabel = "文件服务" },
                    new ServerModel { Tag = "扩展服务", Id = "ADAPTER", SiteLabel = "ERP对接服务" },
                    new ServerModel { Tag = "扩展服务", Id = "JOBCENTER", SiteLabel = "定时任务中心" }
                };
                await DbManager.DbContext.Insertable(arr).ExecuteCommandAsync();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        protected override void OnReady() {
            // 在此处进行浏览器相关操作
            var fevent = new FormiumEvent(this);

            //ExecuteJavaScript("alert('Hello NanUI')");

            //注册 JavaScript 对象
            var jsObject = new JavaScriptObject();
            fevent.RegisterMethod<string>(jsObject, "OpenServerWindow", OpenServerWindow);
            fevent.RegisterMethod(jsObject, "OpenSystemWindow", OpenSystemWindow);
            //然后将这个对象注册到 JavaScript 环境的Formium.external里，并取名launcher。
            RegisterJavaScriptObject("mainwindow", jsObject);

            LoadEnd += PageLoadEnd;
            //注册公共事件
            fevent.RegisterCommonEvent();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PageLoadEnd(object? sender, LoadEndEventArgs e) {
            if (e.Frame.IsMain) {
                SplashScreen.Hide();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        private void OpenSystemWindow() {
            //要跳转到的Formium主窗体
            SystemSettingWindow form2 = new SystemSettingWindow();
            form2.ShowDialog();
        }
        /// <summary>
        /// 
        /// </summary>
        private void OpenServerWindow(string page) {
            //要跳转到的Formium主窗体
            ServerWindow form2 = new(page);
            form2.ShowDialog();
        }
    }
}
